<template>
    <el-form ref="form" :model="form" label-width="80px" class="form">
      <el-form-item label="单位名称">
        <el-input v-model="form.name" class="store_input"></el-input>
      </el-form-item>
      <el-form-item label="联 系 人">
        <el-input v-model="form.user_name" class="store_input"></el-input>
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input v-model="form.phone" class="store_input"></el-input>
      </el-form-item>
      <el-form-item label="单位地址">
        <el-input type="textarea" v-model="form.address" class="store_input"></el-input>
      </el-form-item>
      <el-form-item label="商户logo">
          <el-upload
          class="avatar-uploader"
          :action="apiUrl"
          name="img"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.logo" :src="imageUrl + form.logo" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">更新信息</el-button>
      </el-form-item>
</el-form>
</template>
 
<script>
export default {
  name: 'storeInfo',
  data() {
    return {
      apiUrl:this.apiUrl+'public/upload',
      imageUrl:this.url,
      form: {
      }
    }
  },
  created: function() {
    this.getInfo()
  },
  methods: {
    getInfo:function(){
      var data = {store_id:localStorage.getItem('store_id')}
      var that = this
      this.$get('store/info',data).then((res) => {
        that.form = res.data
      })
    },
    handleAvatarSuccess(res, file) {
      //this.imageUrl = URL.createObjectURL(file.raw);
      this.form.logo = res.data
    },
    beforeAvatarUpload:function(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    onSubmit:function(){
      var validate = {
        name:'商户名称不能为空',
        user_name:'联系人不能为空',
        phone:'联系电话不能为空',
        address:'地址不能为空',
        logo:'商品图片,必须上传'
      }
      for(var x in validate){
        if(!this.form[x]){
          this.$message.error(validate[x]);
          return false;
          break;
        }
      }
      var that = this
      this.$post('store/save',this.form).then((res) => {
          if(res.code == 1){
            that.$message.success(res.msg);
          }else{
            that.$message.error(res.msg);
          }
      })
    }
  }
}
</script>
<style>
.form{
  padding:20px 20px 20px 20%;
  justify-content:center;
}
.store_input {
  width:350px !important;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }
  .avatar {
    width: 120px;
    height: 120px;
    display: block;
  }
</style>